package com.xc.sys.controller;

import com.xc.common.tool.Helper;
import com.xc.db.tables.SysLoginLog;
import com.xc.sys.entity.SysLoginLogEntity;
import com.xc.sys.entity.SysUserEntity;
import com.xc.sys.service.SysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.List;

@Controller
public class RootController {
	@Value("${server.servlet.context-path}")
	private String ctxpath;
	@Autowired
	private SysLoginLogService loginLogService;
	private SysLoginLog LOGINLOG = SysLoginLog.SYS_LOGIN_LOG.as("LOGINLOG");
	@RequestMapping(value = { "/rooter" }, method = { RequestMethod.GET })
	public ModelAndView rooter(String url, Model model, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView(url);
		SysUserEntity me = (SysUserEntity) request.getSession().getAttribute("me");
		if (me == null) {
			mav.setViewName("login-again");
			return mav;
		}
		String l = (String) request.getSession().getAttribute("LANG");
		mav.addObject("l", l);
		mav.addObject("ctxpath", this.ctxpath);
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			mav.addObject(name, request.getParameter(name));
		}
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime now = LocalDateTime.now();
		mav.addObject("thisYear", Integer.valueOf(now.getYear()));
		mav.addObject("thisMonth", Integer.valueOf(now.getMonthValue()));
		mav.addObject("today", fmt.format(now));
		mav.addObject("currentTime", fmt2.format(now));
		return mav;
	}
	


	@RequestMapping({ "/logout" })
	public ModelAndView layout(Model model, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("redirect:login");
		List<SysLoginLogEntity> loginLogs = loginLogService.select(LOGINLOG.SESSION_ID.eq(request.getSession().getId()));
		if(loginLogs.size()==1){
			SysLoginLogEntity loginLog = loginLogs.get(0);
			loginLog.setLogoutTime(LocalDateTime.now());
			loginLog.setState("退出");
			loginLogService.update(loginLog);
		}
		request.getSession().invalidate();
		mav.addObject("ctxpath", this.ctxpath);
		return mav;
	}

	@RequestMapping({ "/decorator/default" })
	public ModelAndView defaultDecorator(Model model, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("decorator/default");
		return mav;
	}
	@RequestMapping({ "/console" })
	public ModelAndView console(Model model, HttpServletRequest request) {
		String lastDate = Helper.getThisMonthLastDate();
		ModelAndView mav = new ModelAndView("console");
		LocalDate now = LocalDate.now();
		DateTimeFormatter fm = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		mav.addObject("today", now.format(fm));
		SysUserEntity me = (SysUserEntity) request.getSession().getAttribute("me");
		if (me == null) {
			mav.setViewName("login-again");
			return mav;
		}
		mav.addObject("lastDate",lastDate);
		return mav;
	}

	@RequestMapping({ "/locate" })
	public ModelAndView locate(String url, Model model, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView(url);
		mav.addObject("ctxpath", this.ctxpath);
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			mav.addObject(name, request.getParameter(name));
		}
		LocalDateTime now = LocalDateTime.now();
		mav.addObject("thisYear", Integer.valueOf(now.getYear()));
		mav.addObject("thisMonth", Integer.valueOf(now.getMonthValue()));
		return mav;
	}


	@RequestMapping({ "/getHeadImage" })
	public String getHeadImage(HttpServletRequest request) {
		// BiHeadPortraitEntity head =
		// this.biHeadPortraitService.getEntityById("3c4691a47aed4e20b503ed798c043ed8");
		String image = "images/portrait.png";
		return image;
	}
}
